/** %^SOF $id[MAIN] */

#include "main.h"
#include "eeprom.h"
#include "defines.h"
#include "interface.h"

int main(void)
{
    unsigned int c,InpBuffCur=0,sCmdCount=1;
    char InpBuff[255];
    char sCmd[25], sParam[25],sFlag,sCmdOk=0, sInputType=0, sFailFlag=0;
    volatile uint8_t  *iAddr, x;

    Initilize();

    while(1)
    {
    for(;;)
    {
	#ifdef ENABLE_WDT
    	wdt_reset();
	#endif
        c = uart_getc();
        if ( c & UART_NO_DATA )
        {
		#ifdef ENABLE_WDT
			wdt_reset();
		#endif
        }
        else
        {

			/** --------- @s Error Check ---------- **/
            if ( c & UART_FRAME_ERROR )
            {
                uart_puts_P("Frame Error! ");
				break;
            }
            if ( c & UART_OVERRUN_ERROR )
            {
                uart_puts_P("Overrun Error!");
				break;
            }
            if ( c & UART_BUFFER_OVERFLOW )
            {
                uart_puts_P("Buffer overflow error!");
				break;
            }
			/** --------- Error Check @e ---------- **/



            if(c!=(unsigned char)0x0D)
            {
            	// Add input to input buffer
            	InpBuff[InpBuffCur]=c;
            	InpBuffCur++;
            }
            else
            {
            	_sEmptyUartBuff();

            	ATOMIC_BLOCK(ATOMIC_FORCEON)
            	{
					InpBuff[InpBuffCur]=0x20; //Add a whitespace to ensure old crap dont matter..
					sInputType=0;
					sFailFlag=0;
					sCmdOk=0;

					strupr(InpBuff);

					sscanf(InpBuff,"%c %s ( %s ) ;",&sFlag,sCmd,sParam);

					if(sCmd[strlen(sCmd)-1] == (char)';') //If ends with ';' then there was no space, so lets deleted it.
					{
						sCmd[strlen(sCmd)-1] = 0x00;
					}

					printf("\n\r[ %3u ] ",sCmdCount);

					switch (sFlag) {
						case (char)CMD_CHAR:
								sInputType = CMD_CODE;
								uart_puts_P("CMD");
								break;
						case (char)HELP_CHAR:
								uart_puts_P("HELP");
								sInputType = HELP_CODE;
								break;
						case (char)INFO_CHAR:
								uart_puts_P("INFO");
								sInputType = INFO_CODE;
								break;
						default:
								uart_puts_P("ERROR");
								sFailFlag = 1;
								break;
					}

					printf(" > %s",sCmd);

					_delay_ms(20);

					if(sInputType==CMD_CODE) //////////////////////////////// CMD_CODE ////////////////////
					{
						if(strcmp(sCmd,"TOGGLE")==0)
						{
							if(GetASwitch())
							{
								SetASwitch_Off();
							} else {
								SetASwitch_On();
							}

							printf("\n\rASwitch %s",((GetASwitch())? "ON" : "OFF" ));
							sCmdOk=1;
						}

						if(strcmp(sCmd,"RESET")==0)
						{
							_reset();
							sCmdOk=1;
						}

					} else if (sInputType==HELP_CODE) { //////////////////////////////// CMD_HELP ////////////////////
						//sParam
						if(strcmp(sCmd,"PARAM")==0)
						{
							//c=uart_getc();
							printf("\n\rParam: %s [%u]",sParam, strlen(sParam));
							sCmdOk=1;
						}

						if(strcmp(sCmd,"HELP")==0)
						{
							wdt_disable();
							//printf("\n\r \t* \t%s - %s",h_cmds[0], h_help[0]);
							//printf("\n\r \t* \t%s - %s",h_cmds[1], h_help[1]);

							//printf("\n\r \t* \t%s - %s",h_cmds[2], h_help[2]);
							printf("\n\r \t* \t%s - %s",h_cmds[3], h_help[3]);
							//printf("\n\r \t* \t%s - %s",h_cmds[4], h_help[4]);

							wdt_enable(ENABLE_WDT);
							sCmdOk=1;
						}

					} else if (sInputType==INFO_CODE) { //////////////////////////////// CMD_INFO ////////////////////

						if(strcmp(sCmd,"GET")==0)
						{
							iAddr   = (unsigned char *)((unsigned int)u2h(atoi(sParam)));
							x = *iAddr;
							printf("\n\rGET 0x%02X = 0x%02X",(u2h(atoi(sParam))),x);
							sCmdOk=1;
						}

					} else {
						printf("\n\rSyntax Error!");
					}

					if(!sCmdOk) {
						printf("\n\rCommand not found...");
					}

					memset(InpBuff,0x00,strlen(InpBuff)); //CLEAR INPBUFF?
					memset(sCmd,0x00,strlen(sCmd));
					memset(sParam,0x00,strlen(sParam));

					InpBuffCur=0;
					sCmdCount++;

            	}
            }
		}
    }
	}
    _reset();
    return 0;
}

void Initilize(void)
{



	uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU));

	sei();

	uart_clr();

	uart_puts_P(_APP_TITLE_);
	_delay_ms(10);
	printf(" (v%s.%s)\n[%s @ %s]",_APP_VER_,_APP_REV_,__DATE__,__TIME__);
	_delay_ms(10);

	#ifdef ENABLE_WDT
		wdt_enable(ENABLE_WDT);
	#endif

	_delay_ms(10);
	uart_puts_P("\n\r\n\r** Welcome, System has been initialized and ready for use...\n\r");
	_delay_ms(20);

}

void HelpDialog(void)
{
	nop();
}

